package cn.smartorigin.soas.frame.xml;

import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JMenuItem;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import cn.smartorigin.soas.frame.menubar.CellMenu;
import cn.smartorigin.soas.frame.menubar.CellMenuBar;
import cn.smartorigin.soas.frame.menubar.CellMenuItem;
import cn.smartorigin.soas.frame.menubar.CellRootMenu;

public class XmlUtil {
	
	/**
	 * 从XML文档中读取 MENU 的配置
	 * 
	 * @param xml
	 * @param action
	 * @return
	 */
	public static CellMenuBar loadMenuBar(String xml, ActionListener action) {
		CellMenuBar menuBar = null;

		try {
			DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document doc = db.parse(xml);				// xml tree model

			Element root = doc.getDocumentElement();
			NodeList rootMenus = root.getChildNodes();	// root menu list

			if (rootMenus != null) {
				menuBar = new CellMenuBar();
				for (int i = 0; i < rootMenus.getLength(); i++) {
					org.w3c.dom.Node menu = rootMenus.item(i);
					if (menu.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
						
						// add  root menu
						if (menu.getNodeName().equalsIgnoreCase("menu")) {
							String text = getStringAttribute(menu, "text");
							CellRootMenu rootMenu = new CellRootMenu();
							rootMenu.setText(text);
							menuBar.add(rootMenu);

							//add menu item
							processMenuItem(menu, rootMenu, action);
						}
						
						// add logo
						if (menu.getNodeName().equalsIgnoreCase("logo")) {
                            String tooltip = getStringAttribute(menu, "tooltip");
                            String imageURL = getStringAttribute(menu, "image");
                            //add company logo here
                            menuBar.add(Box.createGlue());
                            JLabel label = new JLabel(new ImageIcon(imageURL));
                            label.setBorder(BorderFactory.createEmptyBorder(0, 5, 0, 5));
                            label.setToolTipText(tooltip);
                            menuBar.add(label);
						}
					}
				}
			}

		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		}catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return menuBar;
	}
    
    private static void processMenuItem(org.w3c.dom.Node menu, JMenuItem parentMenu, ActionListener action) {
    	//menu items of a menu.
        NodeList children = menu.getChildNodes();
        
        if (children != null) {
            for (int j = 0; j < children.getLength(); j++) {
            	 org.w3c.dom.Node itemNode = children.item(j);
            	 if (itemNode.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
                     boolean isMenuItem = itemNode.getNodeName().equalsIgnoreCase("menuitem");
                     boolean isMenu = itemNode.getNodeName().equalsIgnoreCase("menu");
                     if (isMenuItem || isMenu) {
                         String text = getStringAttribute(itemNode, "text");
                         String tooltip = getStringAttribute(itemNode, "tooltip");
                         Icon icon = getIconAttribute(itemNode, "icon");
                         String command = getStringAttribute(itemNode, "action");

                         JMenuItem menuItem = null;

                         if (isMenu) {
                             menuItem = new CellMenu();
                         } else {
                             menuItem = new CellMenuItem();
                             menuItem.addActionListener(action);
                         }
                         menuItem.setText(text);
                         menuItem.setToolTipText(tooltip);
                         menuItem.setActionCommand(command);
                         menuItem.setIcon(icon);
                         parentMenu.add(menuItem);

                         if (isMenu) {
                             processMenuItem(itemNode, menuItem, action);
                         }
                     }
            	 }
            }
        }
    }
            
	private static String getStringAttribute(org.w3c.dom.Node node, String name) {
		org.w3c.dom.Node attribute = node.getAttributes().getNamedItem(name);
		if (attribute != null) {
			return attribute.getNodeValue();
		} else {
			return null;
		}
	} 
	
    private static Icon getIconAttribute(org.w3c.dom.Node node, String name) {
        String iconURL = getStringAttribute(node, name);
        if (iconURL != null && !iconURL.isEmpty()) {
            return new ImageIcon(iconURL);
        }
        return null;
    }
}
